<template>
  <FormView
    v-model="entity"
    :title="entity.id ? '修改合同信息' : '新建合同信息'"
    :toolbar-attrs="{ buttons: ['back', 'save', 'cancel'] }"
    :toolbar-listeners="{ save: persist }">
    <FormHolder ref="form" label-width="110px" :model="entity">
      <FormItemGroup block-title="基本信息">
        <FormInput
          v-model="entity.budgetNumber"
          prop="budgetNumber"
          legacy
          value-prop="id"
          label-prop="name"
          label="预算">
          <template #append>
            <ElButton type="button" style="padding-right: 10px" @click="isShowBudgetShow = true">选择</ElButton>
          </template>
        </FormInput>
        <FormInput v-model="entity.contractId" prop="contractId" label="合同编号" max-length="255" />
        <FormInput v-model="entity.name" :span="24" prop="name" label="合同名称" max-length="255" required />
        <FormTreeSelect v-model="entity.supplierId" label="供 应 商" :options="vsupplierTreeList" prop="supplierId" />

        <FormSelect
          v-model="entity.type"
          prop="type"
          label="类型"
          dict-type="contract_type_host"
          value="CONTRACT"
          required
          disabled />
        <FormSelect v-model="entity.status" prop="status" label="状态" dict-type="contract_status" required />
        <FormSelect
          v-model="entity.contractType"
          prop="contractType"
          label="合同类型"
          dict-type="contract_type_rate"
          required />
        <FormInput
          v-model="entity.contractSignplace"
          prop="contractSignplace"
          label="签订地点"
          max-length="255"
          required />
        <FormDatePicker v-model="entity.contractSigndate" label="签订时间" prop="contractSigndate" required />
        <FormDatePicker v-model="entity.effectiveDate" label="生效时间" prop="effectiveDate" required />
        <FormDatePicker v-model="entity.expirationDate" label="结束时间" prop="expirationDate" required />
        <FormInputNumber v-model="entity.amount" prop="amount" label="金额" :min="0" :max="999999999" required />
        <FormInput v-model="entity.responsemanName" prop="useName" readonly label="签订人员">
          <template #append>
            <ElButton type="button" style="padding-right: 10px" @click="isShowUserPicker = true">选择</ElButton>
          </template>
        </FormInput>
        <FormTextarea v-model="entity.terms" rows="5" prop="terms" label="条款" max-length="3000" />
        <FormTextarea v-model="entity.remarks" rows="5" prop="remarks" label="备注" max-length="3000" />
      </FormItemGroup>
      <FormItemGroup :gutter="0" block-title="附件信息">
        <UploadPage
          v-if="entity.unid"
          ref="UploadPage"
          style="padding: 0"
          :unique-code="entity.unid"
          model="purchaseOrder"
          classify="purchaseOrder" />
      </FormItemGroup>
    </FormHolder>
    <!-- 选择用户 -->
    <UserPicker
      v-if="isShowUserPicker"
      :reserve-position-on-close="false"
      close-on-click-modal
      :visible.sync="isShowUserPicker"
      @confirm="onConfirmUser" />
    <SelectBudgetTable :visible.sync="isShowBudgetShow" @confirm="onBudgetList" />
  </FormView>
</template>

<script>
  import { addObj, putObj, getObj, create } from '@/api/asset/fyassetcontract'
  import form from '@/vue/mixin/form'

  import { supplierTree } from '@/api/asset/fyassetsupplier'

  export default {
    name: 'ContractForm',
    mixins: [form],
    data() {
      return {
        entity: {
          amount: 0,
          budgetId: null,
          budgetNumber: '',
          contractId: '',
          contractSigndate: null,
          contractSignplace: '',
          contractType: '',
          createBy: '',
          createId: null,
          createTime: '',
          deptFullname: '',
          deptId: null,
          effectiveDate: null,
          expirationDate: null,
          id: '',
          month: null,
          name: '',
          remarks: '',
          responsemanId: null,
          responsemanName: '',
          status: '',
          supplierId: null,
          supplierName: '',
          terms: '',
          type: 'CONTRACT',
          unid: '',
          updateBy: '',
          updateTime: '',
          year: null
        },
        vsupplierTreeList: [],
        isShowUserPicker: false,
        isShowBudgetShow: false
      }
    },
    created() {
      this.init()
    },
    methods: {
      create,
      load: getObj,
      save: addObj,
      update: putObj,
      async init() {
        // 供应商
        const vsupplierRes = await supplierTree('V')
        this.vsupplierTreeList = vsupplierRes.data.data
      },
      onConfirmUser(users) {
        const user = users[0]
        this.entity.responsemanId = user.id
        this.entity.responsemanName = user.name
      },
      onBudgetList(budget) {
        this.entity.budgetId = budget.id
        this.entity.budgetNumber = budget.number
        this.entity.budgetName = budget.type
      }
    }
  }
</script>
